﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;


namespace VPLink2013.Batch
{
    public class EmployeeExtraInfo
    {
        private string _urlSite;
        private string _picturesLibUrl;
        private string _myVPLinkUrl;

        public EmployeeExtraInfo(string url, string libUrl, string myvplinkUrl)
        {
            _urlSite = url;
            _picturesLibUrl = libUrl;
            _myVPLinkUrl = myvplinkUrl;
        }

        public void EmployeesLoginsExtraInfoDicos(Dictionary<string, string> dicLoginPicUrl,
            Dictionary<string, string> dicLoginSitePerso)
        {
            try
            {
                using (SPSite site = new SPSite(_urlSite))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList picLib = web.GetList(_picturesLibUrl);
                        if (picLib != null)
                        {
                            SPFieldFile textField = picLib.Fields["Nom"] as SPFieldFile;
                            SPQuery query = new SPQuery()
                            {
                                ViewFields = string.Format(@"<FieldRef Name='{0}' />", textField.InternalName)
                            };
                            Microsoft.Office.Server.ServerContext sc = Microsoft.Office.Server.ServerContext.GetContext((SPSite)site);
                            UserProfileManager upm = new UserProfileManager(sc);
                            SPListItemCollection items = picLib.GetItems(query);
                            foreach (SPListItem item in items)
                            {
                                if (item.File != null)
                                {
                                    string login = item.File.Item.DisplayName;
                                    Console.WriteLine("Traitement login {0}", login);
                                    dicLoginPicUrl.Add(login, string.Format("{0}/{1}", web.Url, item.Url));
                                    string sitePerso = string.Empty;
                                    upm.GetExtraInfoIsManager(login, ref sitePerso, _myVPLinkUrl);
                                    dicLoginSitePerso.Add(login, sitePerso);
                                    //TraceLog.Add(string.Format("Login : {0}", login));
                                    //TraceLog.Add(string.Format("Site perso {0}", sitePerso));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception lors de la recherche de la photo du profile");
                Console.WriteLine("Exception {0}", ex.Message);
                //TraceLog.Add(string.Format("Exception lors de la recherche de la photo du profile"));
                //TraceLog.Add(string.Format("Exception {0}", ex.Message));
            }
        }
    }

    public static class UserProfileExtension
    {
        public static void GetExtraInfoIsManager(this UserProfileManager input, string login, ref string sitePerso, string myvplinkUrl)
        {
            try
            {
                UserProfile up = input.GetUserProfile(login);
                string IdSSP = up[PropertyConstants.UserGuid].Value != null ? up[PropertyConstants.UserGuid].Value.ToString() : string.Empty;
                sitePerso = string.Format("{0}?guid={1}", myvplinkUrl, IdSSP);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception lors de la recherche des info du userprofile : {0}", login);
                Console.WriteLine("Exception {0}", ex.Message);
                //TraceLog.Add(string.Format("Exception lors de la recherche des info du userprofile : {0}", login));
                //TraceLog.Add(string.Format("Exception {0}", ex.Message));
            }
        }
    }
}
